package com.bms.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.bms.dao.LogDao;
import com.bms.entity.Log;
import com.bms.entity.User;
import com.bms.httpModel.Pager;
import com.bms.service.LogService;
import com.bms.utils.DateUtil;

/**
 * Service实现类 - 日志
 */
@Service
public class LogServiceImpl extends BaseServiceImpl<Log, String> implements LogService {

	@Resource
	private LogDao logDao;

	@Resource
	public void setBaseDao(LogDao logDao) {
		super.setBaseDao(logDao);
	}

	/**
	 * @return the logDao
	 */
	public LogDao getLogDao() {
		return logDao;
	}

	/**
	 * @param logDao the logDao to set
	 */
	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}

	public Pager<Log> findByPager(Log bean, Pager<Log> pager, User loginUser) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Log.class);
		if(bean != null){
			//拼接特殊的where
			if(StringUtils.isNotBlank(bean.getStartDate())){
				criteria.add(Restrictions.ge("createDate", DateUtil.parseDate(bean.getStartDate())));
			}
			if(StringUtils.isNotBlank(bean.getEndDate())){
				criteria.add(Restrictions.le("createDate", DateUtil.getEndOfDate(bean.getEndDate())));
			}
		}
		
		/*if(loginUser.getRole().getId() != 4){
			//非管理员，则只显示自身的日志
			criteria.add(Restrictions.eq("logUser", loginUser.getUsername()));
		}*/
		
		pager.setDetachedCriteria(criteria);
		return logDao.findByPager(pager);
	}

}